package com.py.credit.service.impl;

import com.py.credit.common.Page;
import com.py.credit.dao.NetworkMdSearchDao;
import com.py.credit.service.NetworkMdSearchService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class NetworkMdSearchServiceImpl implements NetworkMdSearchService {

    @Autowired
    private NetworkMdSearchDao networkMdSearchDao;

    @Override
    public List<LinkedHashMap> selectGzNameList(String jclb) {
        return networkMdSearchDao.selectGzNameList(jclb);
    }

    @Override
    public List<LinkedHashMap> selectZtmcListForPage(String jclb, String type, String remark, String searchKey, Page page) {
        return networkMdSearchDao.selectZtmcListForPage(jclb, type, remark, searchKey, page);
    }

    @Override
    public int selectHongheimdCount(String jclb,String status) {
        return networkMdSearchDao.selectHongheimdCount(jclb,status);
    }

    @Override
    public List<LinkedHashMap> selectMdList(String jclb, String tybm, String deptId) {
        return networkMdSearchDao.selectMdList(jclb, tybm, deptId);
    }

    @Override
    public LinkedHashMap selectMdInfo(String ajbh) {
        return networkMdSearchDao.selectMdInfo(ajbh);
    }

    @Override
    public List<LinkedHashMap> selectCsMdForPage(String jclb, Page page) {
        return networkMdSearchDao.selectCsMdForPage(jclb, page);
    }

    @Override
    public void excelExport(HttpServletResponse response, String jclb, String gzId, String deptId, String csId, String csName) {
        this.queryTableExecle(response, jclb, gzId, deptId, csId, csName);
    }

    @Override
    public List<LinkedHashMap> selectPhDeptCsNum(String jclb, String name) {
        //判断是否包含所有部门，如果有，则统计
        int allDeptCsNum = networkMdSearchDao.selectAllDeptCsNum(jclb, name);
        if (allDeptCsNum > 0) {
            return networkMdSearchDao.selectAllPhDeptNum(allDeptCsNum, jclb, name);
        } else {
            return networkMdSearchDao.selectPhDeptNum(jclb, name);
        }
    }

    @Override
    public LinkedHashMap selectGzInfo(String jclb, String name) {
        LinkedHashMap map = new LinkedHashMap();
        map.put("gzInfo",networkMdSearchDao.selectGzInfo(jclb, name));

        Page page=new Page();
        page.setPage(1);
        page.setLimit(7);
        map.put("csList", networkMdSearchDao.selectGzCsForPage(jclb, name, page));
        return map;
    }

    @Override
    public List<LinkedHashMap> selectGzCsForPage(String jclb, String name,Page page) {
        return networkMdSearchDao.selectGzCsForPage(jclb, name, page);
    }

    private void queryTableExecle(HttpServletResponse response, String jclb, String gzId, String deptId, String csId, String csName) {
        // 生成提示信息
        response.setContentType("application/vnd.ms-excel");
        HSSFWorkbook workbook = null;
        OutputStream fOut = null;
        String codedFileName = csName;

        if (jclb.equals("C")) {
            codedFileName = codedFileName + "黑名单";
        } else {
            codedFileName = codedFileName + "红名单";
        }

        // 进行转码，使其支持中文文件名
        try {
            codedFileName = URLEncoder.encode(codedFileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");

        // 产生工作簿对象
        workbook = new HSSFWorkbook();
        // 产生工作表对象
        HSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(0, "具体名单数据");
        HSSFRow row = null;
        HSSFCell cell = null;

        String[] strBT = {"事件编号", "主体名称", "编码", "规则名称", "规则详情", "来源部门", "所属区域", "原始案件号", "生效时间", "截止时间"};
        List<Map<String, String>> list = networkMdSearchDao.selectDownloadMdList(jclb, gzId, deptId, csId);

        //创建标题行
        row = sheet.createRow(0);
        for (int i = 0; i < strBT.length; i++) {
            cell = row.createCell(i);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(strBT[i]);
        }

        int count = 1;
        for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow(count);//创建一行

            cell = row.createCell(0);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("ajbh"));

            cell = row.createCell(1);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("ztmc"));

            cell = row.createCell(2);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("bm"));

            cell = row.createCell(3);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("gzName"));

            cell = row.createCell(4);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("gzDetailName"));

            cell = row.createCell(5);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("deptName"));

            cell = row.createCell(6);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("areaName"));

            cell = row.createCell(7);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("ysajh"));

            cell = row.createCell(8);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("sxsj"));

            cell = row.createCell(9);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue(list.get(i).get("repairTime"));

            count++;
        }

        list = null;

        try {
            fOut = response.getOutputStream();
            workbook.write(fOut);
            workbook.close();
            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
